import { Message } from 'element-ui';
import { login } from '@/api/login'
import { getItem, setItem } from '@/utils/storage'
import router from '@/router'
const SHOP_USER = 'SHOP_USER'
export default {
    namespaced:true,
    state: () => ({
        userInfo: getItem(SHOP_USER) || {},
    }),
    mutations: {
        setUserInfo(state, data) {
            state.userInfo = data
            setItem(SHOP_USER, data)
        },
    },
    actions: {
        async login(content, data) {
            let res = await login(data)
            const vm = this
            if (res.meta.status !== 200) {
                // 请求错误处理'

                return Message.error(res.meta.msg)
            }
            // console.log(res);
            content.commit('setUserInfo', res.data)
            router.push("/");
        },
    },
    getters: {
        token: state => state.userInfo.token,
        username: state => state.userInfo.username
    }
}